package com.enjoyor.soa.traffic.rest.tsms.dubbo.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.enjoyor.soa.traffic.api.dto.tsms.ExecuteTaskDispatchDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RltRoutePostDto;
import com.enjoyor.soa.traffic.api.service.dubbo.tsms.IDubboExecuteTaskDispatchService;
import com.enjoyor.soa.traffic.api.service.dubbo.tsms.IDubboRltRoutePostService;
import com.enjoyor.soa.traffic.rest.tsms.dubbo.IExecuteTaskDispatchConsumeService;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Component
public class ExecuteTaskDispatchConsumeService implements IExecuteTaskDispatchConsumeService{

	@Reference(version = "1.0.0", timeout = 10000)
	//@Reference(version = "1.0.0", timeout = 10000, url="dubbo://192.168.56.8:21241")
	private IDubboExecuteTaskDispatchService iDubboExecuteTaskDispatchService;
	
	@Reference(version = "1.0.0", timeout = 10000)
	//@Reference(version = "1.0.0", timeout = 10000, url="dubbo://192.168.56.8:21241")
	private IDubboRltRoutePostService iDubboRltRoutePostService;
	
	@Override
	public ResultPojo getExecuteTaskDispatchPage(Page page,String dispatchId, String executeTaskId) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		resultPojo = iDubboExecuteTaskDispatchService.getExecuteTaskDispatchPage(page,dispatchId, executeTaskId);
		return resultPojo;
	}

	@Override
	public ResultPojo getExecuteTaskDispatchList(String dispatchId, String executeTaskId, String postId,
			String policeId, String policeName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		resultPojo = iDubboExecuteTaskDispatchService.getExecuteTaskDispatchList(dispatchId, executeTaskId, postId,
				policeId, policeName);
		return resultPojo;
	}

	@Override
	public ResultPojo getPostPoliceDispatchList(String taskId, String executeTaskId, String routeId) {
		//ResultPojo resultPojo = new ResultPojo();
		ResultPojo dispatchResult = iDubboExecuteTaskDispatchService.getExecuteTaskDispatchList(null, executeTaskId,null,null,null);
		List<ExecuteTaskDispatchDto> dispatchDtoList = (List<ExecuteTaskDispatchDto>)dispatchResult.getResultList();
		ResultPojo postResult = iDubboRltRoutePostService.getRouteRltPostList(taskId, routeId, null, null);
		List<RltRoutePostDto> postDtoList = (List<RltRoutePostDto>)postResult.getResultList();
		if(null != postDtoList && postDtoList.size() > 0) {
			
			List<ExecuteTaskDispatchDto> dispatchNullList = new ArrayList<ExecuteTaskDispatchDto>();
			if(null != dispatchDtoList && dispatchDtoList.size() > 0) {
				for(ExecuteTaskDispatchDto dispatchDto : dispatchDtoList) {
					if(null == dispatchDto.getPostId() || "".equals(dispatchDto.getPostId())) {
						dispatchNullList.add(dispatchDto);
					} else {
						for(int i = 0; i < postDtoList.size(); i++) {
							RltRoutePostDto postDto = postDtoList.get(i);
							if(postDto.getPostId().equals(dispatchDto.getPostId())) {
								List<ExecuteTaskDispatchDto> tempDto = postDto.getExecuteTaskDispatchList();
								if(null == tempDto) {
									tempDto = new ArrayList<ExecuteTaskDispatchDto>();
								}
								tempDto.add(dispatchDto);
								postDto.setExecuteTaskDispatchList(tempDto);
								break;
							}
						}
					}
				}
			}
			RltRoutePostDto postNullDto = new RltRoutePostDto();
			postNullDto.setPostName("空岗位");
			postNullDto.setExecuteTaskDispatchList(dispatchNullList);
			postDtoList.add(postNullDto);
		}
		return ResultHelper.getResult(postDtoList);
	}
	
	@Override
	public ResultPojo addExecuteTaskDispatch(ExecuteTaskDispatchDto executeTaskDispatchDto, String personName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = iDubboExecuteTaskDispatchService.addExecuteTaskDispatch(executeTaskDispatchDto, personName);
		return resultPojo;
	}

	@Override
	public ResultPojo editExecuteTaskDispatch(ExecuteTaskDispatchDto executeTaskDispatchDto, String personName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		resultPojo = iDubboExecuteTaskDispatchService.editExecuteTaskDispatch(executeTaskDispatchDto, personName);
		return resultPojo;
	}

	@Override
	public ResultPojo deleteExecuteTaskDispatch(String dispatchId, String personName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		resultPojo = iDubboExecuteTaskDispatchService.deleteExecuteTaskDispatch(dispatchId, personName);
		return resultPojo;
	}

	@Override
	public ResultPojo assignExecuteTaskDispatch(List<ExecuteTaskDispatchDto> list, String executeTaskId,
			String personName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		//将字符串数组转化为List对象集合；
		//List<ExecuteTaskDispatchDto> ExecuteTaskDispatchList = JSONArray.parseArray(list, ExecuteTaskDispatchDto.class);
		resultPojo = iDubboExecuteTaskDispatchService.assignExecuteTaskDispatch(list, executeTaskId, personName);
		return resultPojo;
	}

}
